<?php
/**
 * Created by PhpStorm.
 * User: liujun
 * Date: 2017/11/8
 * Time: 下午4:45
 */

use Dai\Framework\Library\Trace;
use Phalcon\Di\FactoryDefault;

error_reporting(E_ALL);

define('APP_PATH', dirname(__DIR__));
define('BASE_PATH', APP_PATH . '/');
define('CONFIG_PATH', APP_PATH . '/config/');
define('PRJ_NS', 'PddWechat');
define('IMAGE_PATH', '/home/work/data/image/');


require_once BASE_PATH . "/vendor/autoload.php";

//set_exception_handler('myException');

try {
    /**
     * The FactoryDefault Dependency Injector automatically registers
     * the services that provide a full stack framework.
     */
    $di = new FactoryDefault();

    /**
     * Service init
     */
    (new \Dai\Framework\Init\Service()) ->init($di);

    /**
     * Get config service for use in inline setup below
     */
    $config = \Dai\Framework\Library\ConfigLibrary::get("config", "application");

    /**
     * Include Autoloader
     */
    $loader = new \Phalcon\Loader();
    (new \Dai\Framework\Init\Loader()) ->init($loader, $config);

    /**
     * Handle the request
     */
    $application = new \Phalcon\Mvc\Application($di);
    echo str_replace(["\n","\r","\t"], '', $application->handle()->getContent());
} catch (\Dai\Framework\Base\BaseException $e) {
    $data = [
        'code' => $e->getCode(),
        'msg' => $e->getMessage(),
        'data' => [],
    ];
    \Dai\Framework\Library\Log::error(sprintf("fatal error, code %d, msg %s, file %s, trace %s",  $e->getCode(), $e->getMessage(), $e->getFile().":".$e->getLine(),  $e->getTraceAsString() ));
    echo json_encode($data, JSON_UNESCAPED_UNICODE);exit;
}

/**
 * @param Exception $exception
 */
function myException($exception)
{
    if( Trace::getInstance()::getValid() == true  || \Dai\Framework\Library\Runmode::get() != "online") {
        $data = [
            'code' => $exception->getCode(),
            'msg' => $exception->getMessage(),
            "file" => $exception->getFile().":".$exception->getLine(),
            'trace' => $exception->getTrace(),
        ];
    }else {
        $data = [
            'code' => -1,
            'msg' => "系统错误",
            'data' => [],
        ];
    }
    \Dai\Framework\Library\Log::error(sprintf("fatal error, code %d, msg %s, file %s, trace %s",  $exception->getCode(), $exception->getMessage(), $exception->getFile().":".$exception->getLine(),  $exception->getTraceAsString() ));
    echo json_encode($data, JSON_UNESCAPED_UNICODE);exit;
}